﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using E2_WEBSITE.Code.Product;
using Code.Manufacture;
using System.Data;
using E2_WEBSITE.Code;

public partial class Products_compare : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.LoadComplete += new EventHandler(Page_LoadComplete);
        if (IsPostBack)
        {
            if (cbProductType1.SelectedIndex > 0)
            {
                if (cbManufacture1.Enabled == false)
                {
                    cbManufacture1.Enabled = true;
                    string sCatelogyName = cbProductType1.Text;

                    ManufactureController mcl = new ManufactureController();
                    int iId = mcl.GetManufactureIDByName(sCatelogyName);
                    List<string> sManuName = mcl.GetManuFactureNameByCatelogyID(iId);

                    cbManufacture1.Items.Clear();
                    cbManufacture1.Items.Add("-Chọn Hãng Sản Xuất-");
                    foreach (string s in sManuName)
                    {
                        cbManufacture1.Items.Add(s);
                    }
                }
                else
                {
                    if (cbManufacture1.SelectedIndex > 0)
                    {
                        cbProduct1.Enabled = true;
                        ProductController pcl = new ProductController();
                        List<string> lst = pcl.GetListProductNameByManufactureNameAndCatelogyName(cbManufacture1.Text, cbProductType1.Text);
                        bool bLoad = false;
                        if (cbProduct1.Enabled)
                        {
                            if (cbProduct1.SelectedIndex > 0)
                            {
                                if (lst.Contains(cbProduct1.Text))
                                {
                                    bLoad = true;
                                    int iID = pcl.GetProductIdByProductNameAndCatelogyAndManufacture(cbProduct1.Text, cbManufacture1.Text, cbProductType1.Text);
                                    if (iID != -1)
                                    {
                                        DataTable dt = new DataTable();
                                        ProductInfo pInfo = pcl.getProductDetail(iID.ToString(), cbProductType1.Text, out dt);

                                        ProductInfo pMainInfo = pcl.getProductById(iID);
                                        lbPrice1.Text = "Giá : " + Utilities.ConvertToMoneyFormat(pInfo.Gia,true);
                                        lbManufacture1.Text = "Nhà SX : "+ pMainInfo.NSX.TenNSX;
                                        lbWarranty1.Text = "Bảo hành : "+ pMainInfo.BaoHanh;
                                        lbNhaPhanPhoi1.Text = "Nhà phân phối : "+pMainInfo.Nha_Phan_Phoi.TenNhanPhanPhoi;
                                       
                                        imgProduct1.ImageUrl = pInfo.Hinh_Anh.URL;

                                        if (dt.Rows.Count > 0)
                                        {


                                            int iColumnCount = dt.Columns.Count;
                                            for (int i = 0; i < iColumnCount; i++)
                                            {
                                                TableRow tbRow = new TableRow();
                                                TableCell tbCell1 = new TableCell();
                                                tbCell1.CssClass = "Table_Odd_Column";
                                                TableCell tbCell2 = new TableCell();
                                                tbCell2.CssClass = "Table_Even_Column";
                                                Label lbInfo = new Label();
                                                lbInfo.CssClass = "Table_Even_Column";
                                                lbInfo.Text = dt.Columns[i].ColumnName + " : " + dt.Rows[0][i].ToString();
                                                tbCell2.Controls.Add(lbInfo);

                                                TableCell tbCell3 = new TableCell();
                                                tbCell3.CssClass = "Table_Odd_Column";
                                                Label lbInfo2 = new Label();
                                                lbInfo2.CssClass = "Table_Odd_Column";
                                                tbCell3.Controls.Add(lbInfo2);

                                                tbRow.Cells.Add(tbCell1);
                                                tbRow.Cells.Add(tbCell2);
                                                tbRow.Cells.Add(tbCell3);
                                                Table1.Rows.AddAt(Table1.Rows.Count, tbRow);
                                            }
                                        }
                                    }

                                }
                            }
                        }
                        if (!bLoad)
                        {
                            cbProduct1.Items.Clear();
                            cbProduct1.Items.Add("-Chọn Sản Phẩm-");
                            foreach (string s in lst)
                            {
                                cbProduct1.Items.Add(s);
                            }
                        }

                    }

                }
            }

            if (cbProductType2.SelectedIndex > 0)
            {


                if (cbManufacture2.Enabled == false)
                {
                    cbManufacture2.Enabled = true;
                    string sCatelogyName = cbProductType2.Text;

                    ManufactureController mcl = new ManufactureController();
                    int iId = mcl.GetManufactureIDByName(sCatelogyName);
                    List<string> sManuName = mcl.GetManuFactureNameByCatelogyID(iId);

                    cbManufacture2.Items.Clear();
                    cbManufacture2.Items.Add("-Chọn Hãng Sản Xuất-");
                    foreach (string s in sManuName)
                    {
                        cbManufacture2.Items.Add(s);
                    }
                }
                else
                {
                    if (cbManufacture2.SelectedIndex > 0)
                    {
                        cbProduct2.Enabled = true;
                        ProductController pcl = new ProductController();
                        List<string> lst = pcl.GetListProductNameByManufactureNameAndCatelogyName(cbManufacture2.Text, cbProductType2.Text);
                        bool bLoad = false;
                        if (cbProduct2.Enabled)
                        {
                            if (cbProduct2.SelectedIndex > 0)
                            {
                                if (lst.Contains(cbProduct2.Text))
                                {
                                    bLoad = true;
                                    int iID = pcl.GetProductIdByProductNameAndCatelogyAndManufacture(cbProduct2.Text, cbManufacture2.Text, cbProductType2.Text);
                                    if (iID != -1)
                                    {
                                        DataTable dt = new DataTable();
                                        ProductInfo pInfo = pcl.getProductDetail(iID.ToString(), cbProductType2.Text, out dt);

                                        ProductInfo pMainInfo = pcl.getProductById(iID);
                                        lbPrice2.Text = "Giá : " + Utilities.ConvertToMoneyFormat(pInfo.Gia, true);
                                        lbManufacture2.Text = "Nhà SX : " + pMainInfo.NSX.TenNSX;
                                        lbWarranty2.Text = "Bảo hành : " + pMainInfo.BaoHanh;
                                        lbPhaPhanPhoi2.Text = "Nhà phân phối : " + pMainInfo.Nha_Phan_Phoi.TenNhanPhanPhoi;
                                       
                                        imgProduct2.ImageUrl = pInfo.Hinh_Anh.URL;

                                        int iColumnCount = dt.Columns.Count;
                                        int iTableCount = Table1.Rows.Count;
                                        if (dt.Rows.Count > 0)
                                        {


                                            for (int i = 0; i < iColumnCount; i++)
                                            {
                                                string sText = dt.Columns[i].ColumnName + " : " + dt.Rows[0][i].ToString();
                                                ((Label)Table1.Rows[iTableCount - (iColumnCount - i)].Cells[2].Controls[0]).Text = sText;
                                            }
                                        }

                                    }
                                }
                            }
                        }
                        if (!bLoad)
                        {
                            cbProduct2.Items.Clear();
                            cbProduct2.Items.Add("-Chọn Sản Phẩm-");
                            foreach (string s in lst)
                            {
                                cbProduct2.Items.Add(s);
                            }
                        }

                    }

                }
            }



        }
        else
        {
            string sID = Request.QueryString["ProID"];
            if (sID != null)
            {
                int iID = int.Parse(sID);
                ProductController pcl = new ProductController();
                ProductInfo pInfo = pcl.getProductById(iID);
                if (pInfo != null)
                {
                    cbProductType1.Items.Clear();
                    cbProductType2.Items.Clear();
                    cbProductType1.Items.Add("-Chọn Loại Sản Phẩm-");
                    cbProductType2.Items.Add("-Chọn Loại Sản Phẩm-");
                    cbProductType1.Items.Add(pInfo.Loai_San_Pham.TenLoaiSanPham);
                    cbProductType2.Items.Add(pInfo.Loai_San_Pham.TenLoaiSanPham);
                    cbProductType1.SelectedIndex = 1;
                    cbProductType2.SelectedIndex = 1;
                    cbProductType1.Enabled = false;
                    cbProductType2.Enabled = false;
                    ManufactureController mcl = new ManufactureController();
                    List<string> sManuName = mcl.GetManuFactureNameByCatelogyID(pInfo.Loai_San_Pham.ID);
                    cbManufacture1.Items.Clear();
                    cbManufacture2.Items.Clear();
                    cbManufacture1.Items.Add("-Chọn Hãng-");
                    cbManufacture2.Items.Add("-Chọn Hãng-");
                    foreach (string s in sManuName)
                    {
                        cbManufacture1.Items.Add(s);
                        cbManufacture2.Items.Add(s);
                        //cbManufacture1.Text = pInfo.NSX.TenNSX;

                    }
                    cbManufacture1.SelectedValue = pInfo.NSX.TenNSX;

                    cbProduct1.Enabled = true;
                    cbProduct1.Items.Clear();
                    cbProduct2.Items.Clear();
                    cbProduct1.Items.Add("-Chọn Sản Phẩm-");
                    cbProduct2.Items.Add("-Chọn Sản Phẩm-");
                    cbProduct2.Enabled = false;



                    List<string> lst = pcl.GetListProductNameByManufactureNameAndCatelogyName(cbManufacture1.Text, cbProductType1.Text);
                    foreach (string s in lst)
                    {
                        cbProduct1.Items.Add(s);
                    }
                    
                    DataTable dtDetail = new DataTable();
                    ProductInfo pInfoDetail = pcl.getProductDetail(iID.ToString(), cbProductType1.Text, out dtDetail);
                    cbProduct1.SelectedValue = pInfoDetail.TenSanPham;


                    lbPrice1.Text = "Giá : " + Utilities.ConvertToMoneyFormat(pInfoDetail.Gia, true);
                    lbManufacture1.Text = "Nhà SX : " + pInfo.NSX.TenNSX;
                    lbWarranty1.Text = "Bảo hành : " + pInfo.BaoHanh;
                    lbNhaPhanPhoi1.Text = "Nhà phân phối : " + pInfo.Nha_Phan_Phoi.TenNhanPhanPhoi;

                    imgProduct1.ImageUrl = pInfoDetail.Hinh_Anh.URL;

                    if (dtDetail.Rows.Count > 0)
                    {
                        int iColumnCount = dtDetail.Columns.Count;
                        for (int i = 0; i < iColumnCount; i++)
                        {
                            TableRow tbRow = new TableRow();
                            TableCell tbCell1 = new TableCell();
                            tbCell1.CssClass = "Table_Odd_Column";
                            TableCell tbCell2 = new TableCell();
                            tbCell2.CssClass = "Table_Even_Column";
                            Label lbInfo = new Label();
                            lbInfo.CssClass = "Table_Even_Column";
                            lbInfo.Text = dtDetail.Columns[i].ColumnName + " : " + dtDetail.Rows[0][i].ToString();
                            tbCell2.Controls.Add(lbInfo);

                            TableCell tbCell3 = new TableCell();
                            tbCell3.CssClass = "Table_Odd_Column";
                            Label lbInfo2 = new Label();
                            lbInfo2.CssClass = "Table_Odd_Column";
                            tbCell3.Controls.Add(lbInfo2);

                            tbRow.Cells.Add(tbCell1);
                            tbRow.Cells.Add(tbCell2);
                            tbRow.Cells.Add(tbCell3);
                            Table1.Rows.AddAt(Table1.Rows.Count, tbRow);
                        }
                    }

                }
            }
        }
    }


    void Page_LoadComplete(object sender, EventArgs e)
    {
        

    }
}
